<template>
    <div class="change-password-modal">
      <div class="modal-content">
        <h3>修改密码</h3>
        <input type="password" v-model="oldPassword" placeholder="旧密码" />
        <input type="password" v-model="newPassword" placeholder="新密码" />
        <input type="password" v-model="confirmPassword" placeholder="确认新密码" />
        <div class="buttons">
          <button @click="submit">提交</button>
          <button @click="close">取消</button>
        </div>
      </div>
    </div>
  </template>
  
  <script lang="ts">
  import { defineComponent, reactive, toRefs } from 'vue';
  import { api_changePassword } from '@/api/user';
  import { message } from 'ant-design-vue';
  import { useStore } from 'vuex'; // 引入 Vuex
  export default defineComponent({
    name: 'ChangePasswordModal',
    setup(props, { emit }) {
      const store = useStore();
      const state = reactive({
        oldPassword: '',
        newPassword: '',
        confirmPassword: '',
      });
  
      const submit = async () => {
        if (state.newPassword !== state.confirmPassword) {
          message.error('新的密码和确认密码不一样');
          return;
        }
        try {


          const username = store.state.userInfo.userName; // 从 Vuex store 中获取 username
          const userId = store.state.userInfo.userId;
          let res = await api_changePassword({
            userName:username,
            userId:userId,
            oldPassword: state.oldPassword,
            newPassword: state.newPassword ,
          });
          console.log('Username:', username);
          console.log('UserId:', userId);
          console.log('Old Password:', state.oldPassword);
          console.log('New Password:', state.newPassword);
          console.log('API Response:', res);
          if (res.code === 200) {
            message.success('密码修改成功');
            emit('close');
          } else {
            message.error(res.msg || '密码修改失败');
          }
        } catch (error) {
          message.error('密码修改失败');
        }
      };
  
      const close = () => {
        emit('close');
      };
  
      return {
        ...toRefs(state),
        submit,
        close,
      };
    },
  });
  </script>
  
  <style scoped>
  .change-password-modal {
    position: fixed;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    background: #CCCCCC;
    padding: 50px;
    border-radius: 10px;
    box-shadow: 0 4px 20px rgba(0, 0, 0, 0.2);
    z-index: 1000;
    width: 350px;
  }
  
  .modal-content {
    display: flex;
    flex-direction: column;
    align-items: center;
  }
  
  .modal-content h3 {
    margin-bottom: 20px;
    font-size: 20px;
    color: #333;
  }
  
  .modal-content input {
    width: 100%;
    margin-bottom: 15px;
    padding: 10px;
    border: 1px solid #ccc;
    border-radius: 5px;
    font-size: 14px;
    box-sizing: border-box;
  }
  
  .buttons {
    display: flex;
    justify-content: space-between;
    width: 100%;
  }
  
  .buttons button {
    padding: 10px 15px;
    border: none;
    border-radius: 5px;
    cursor: pointer;
    font-size: 14px;
  }
  
  .buttons button:first-child {
    background: #4caf50;
    color: #fff;
  }
  
  .buttons button:first-child:hover {
    background: #45a049;
  }
  
  .buttons button:last-child {
    background: #f44336;
    color: #fff;
  }
  
  .buttons button:last-child:hover {
    background: #e53935;
  }
  </style>
  